<?php
session_start();
include("../include/config.inc.php");

$todo=$_REQUEST["todo"];

if(isset($todo) && $todo=="todo"){
	$mobile = $_POST['mobile'];
	$smscode = $_POST['smscode'];
	$code = $_POST['code'];
	$verify = $_SESSION['session_verify'];
	$sms = $_SESSION['session_smscode'];
	
	if($code==""){
		echo "<script>alert('请输入验证码！');history.back(-1);</script>";
		exit;
	}
	if($code<>$verify){
		echo "<script>alert('请输入正确的验证码！');history.back(-1);</script>";
		exit;
	}
	if($mobile==""){
		echo "<script>alert('请输入手机号码！');history.back(-1);</script>";
		exit;
	}
	if($smscode==""){
		echo "<script>alert('请输入手机短信验证码！');history.back(-1);</script>";
		exit;
	}
	if($smscode<>$sms){	
		echo "<script>alert('手机短信验证码输入不正确！');history.back(-1);</script>";
		exit;
	}else{
		$sql="select member_id from ".tableprefix."member where member_mobile='$mobile' and member_state='1'";
		$result=$db->query($sql);
		if($db->num_rows($result)==0){
			$ip=getIP();
			$time=time();
			$member_name="YX".$time;
			$member_passwd=md5($time);
			$member_mobile=$mobile;
			$member_mobile_bind='1';
			$member_login_num='1';
			$member_time=$time;
			$member_login_time=$time;
			$member_old_login_time=$time;
			$member_login_ip=$ip;
			$member_old_login_ip=$ip;
			$member_points='20';
			$member_old_login_time=$time;
			$member_provinceid='19';
			$member_areainfo='广东省';
			$sql="insert into ".tableprefix."member(member_name,member_passwd,member_mobile,member_mobile_bind,member_login_num,member_time,member_login_time,member_old_login_time,member_login_ip,member_old_login_ip,member_points,member_provinceid,member_areainfo)values('$member_name','$member_passwd','$member_mobile','$member_mobile_bind','$member_login_num','$member_time','$member_login_time','$member_old_login_time','$member_login_ip','$member_old_login_ip','$member_points','$member_provinceid','$member_areainfo')";
			$db->query($sql);
			if(!$db->error()){
				$member_id = mysql_insert_id();
				$_SESSION[tableprefix.'sys_member_id_2017'] = $member_id;
				echo "<script>location.href='index.php';</script>";
			}else{
				echo "<script>alert('数据库出错，请重新登陆。');location.href='loginbysms.php';</script>";
			}
			$db->close();
			exit;
		}else{
			$row=$db->fetch_array($result);
			$member_id = $row["member_id"];
			$_SESSION[tableprefix.'sys_member_id_2017'] = $member_id;
			$time=time();
			$ip=GetIP();
			$sql="update ".tableprefix."member set member_login_num=member_login_num+1,member_points=member_points+30,member_exppoints=member_exppoints+5,member_login_ip='$ip',member_login_time='$time' where member_id='$member_id'";
			$db->query($sql);
			if(!$db->error()){
				GotoPage('index.php');
			}else{
				GotoPage('login.php');
			}		
			$db->free_result($result);
			$db->close();
			exit;
		}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
<meta name="format-detection" content="telephone=no" />
<title>手机短信登陆</title>
<link rel="stylesheet" type="text/css" href="../css/login.css">
<script type="text/javascript" src="../js/clientwidth.js"></script>
<script type="text/javascript" src="../js/jquery.js"></script>
<script language="javascript">
	function get_mobile_code(){
        $.post('sms.php', {mobile:jQuery.trim($('#mobile').val()),send_code:jQuery.trim($('#code').val()),smstype:jQuery.trim($('#smstype').val())}, function(msg) {
            alert(jQuery.trim(unescape(msg)));
			if(msg=='短信发送成功'){
				RemainTime();
			}
        });
	};
	var iTime = 59;
	var Account;
	function RemainTime(){
		document.getElementById('zphone').disabled = true;
		var iSecond,sSecond="",sTime="";
		if (iTime >= 0){
			iSecond = parseInt(iTime%60);
			iMinute = parseInt(iTime/60)
			if (iSecond >= 0){
				if(iMinute>0){
					sSecond = iMinute + "分" + iSecond + "秒";
				}else{
					sSecond = iSecond + "秒";
				}
			}
			sTime=sSecond;
			if(iTime==0){
				clearTimeout(Account);
				sTime='获取手机验证码';
				iTime = 59;
				document.getElementById('zphone').disabled = false;
			}else{
				Account = setTimeout("RemainTime()",1000);
				iTime=iTime-1;
			}
		}else{
			sTime='没有倒计时';
		}
		document.getElementById('zphone').value = sTime;
	}	
</script>
</head>

<body>
<header class="header" style="display:block;"><span class="goback"></span>手机短信登陆<span class="close"></span></header>
<form id="login-form" action="" method="post">
<input name="todo" type="hidden" value="todo">
<input name="smstype" id="smstype" type="hidden" value="loginbysms">
<section class="loginpage">
	<div class="loginlogo"><img src="../images/logo.png" /></div>
	<div class="loginform">
		<div class="input-container">
            <input id="mobile" name="mobile" type="text" placeholder="请输入手机号码" maxlength="11" style="border-radius:5px 5px 0px 0px;background-position:0px 3px;">
        </div>
		<div class="input-container">
            <input id="code" name="code" type="text" placeholder="请输入验证码" maxlength="4" style="border-radius:0px;border-top:none;">
            <span class="code" style="position:absolute;top:-4px;right:1px;z-index:1;"><img id="imgCode" src="../include/verify.php" alt="请输入验证码" style="border-radius:0px 0px 0px 0px;float:right;width:100%;height:48px;overflow:hidden;"></span>
        </div>
		<div class="input-container" style="position:relative;">
            <input id="smscode" name="smscode" type="text" placeholder="请输入短信验证码" maxlength="6" style="border-radius:0px 0px 5px 5px;border-top:none;">
            <span class="code-box" style="width:118px;height:28px;overflow:hidden;line-height:28px;position:absolute;top:8px;right:0;z-index:1;background:#009900;border-radius:5px;margin-right:10px;"><input id="zphone" type="button" value="获取手机验证码" onClick="get_mobile_code();" style="text-indent:0px;font-size:14px;outline:medium;background:none;border:0;color:#FF0000;width:108px;height:28px;overflow:hidden;line-height:28px;left:0px;color:#FFFFFF;text-align:center;padding:0px 5px;"></span>
        </div>		
	</div>
	<div class="notice"></div>
	<div class="input-submit"><input name="s" type="submit" class="sub-input" value="登 录" /></div>
    <div class="quick-login">
        <h4>其他登录方式</h4>
        <a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc932d20884c7855b&redirect_uri=http%3A%2F%2Fdx.wycn.com%2Fuser%2Fuserinfo.php&response_type=code&scope=snsapi_userinfo&state=1&connect_redirect=1#wechat_redirect"><i class="icon icon-wx"></i><br>微信</a>
    </div>
</section>
</form>
<script>
if (document.body.clientWidth > 500) {
	$('.loginpage').css({
		width: 500,
		margin: '0 auto',
	});
}
</script>
</body>
</html>